﻿using Furion.DependencyInjection;
using LS.Nc6.Application.System;
using LS.Nc6.Core;
using LS.Nc6.Web.Entry.ViewModel.UI;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections;


namespace LS.Nc6.Web.Entry.Controllers
{
    public class HomeController : Controller
    {
        private readonly ICmsChannelService _cmsChannelService;
        private readonly ICmsBannerService _bannerService;
        private readonly ICmsContentService _contentService;
        private readonly ISysFileService _fileService;
        private  readonly ICache _cache;

        
        public HomeController(ICmsChannelService cmsChannelService,
            ICmsBannerService bannerService,
            ICmsContentService contentService,
            ISysFileService fileService,
            IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed) 
        {
            _cmsChannelService = cmsChannelService;
            _bannerService = bannerService;
            _contentService = contentService;
            _fileService = fileService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
        }

        /// <summary>
        /// UI首页
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> Index()
        {
            var bannerlist=await  _bannerService.List(a => a.BType== "Home", a => a.Taxis);
            ViewBag.BannerList = bannerlist;

            var contentlist= await _contentService.PageDynamicList(a => a.GroupNameCollection.Contains("首页新闻推荐"),1,6, true,a => a.Taxis, a => new 
           {
                a.Id,
                a.Title,
                a.SubTitle,
                a.Summary,
                a.AddTime,
                a.ImageUrlIds
           });
            List<CmsContentVm> recomcontents = new List<CmsContentVm>();
            var recomitems = contentlist.Item1.Where(a => !string.IsNullOrEmpty(a.ImageUrlIds)).Take(3); 
            foreach (var item in recomitems) {
                recomcontents.Add( new CmsContentVm { 
                  Title= item.Title,
                  SubTitle= item.SubTitle,
                  Summary= item.Summary,
                    CoverUrl = ( await _fileService.GetByIdAsync(CommonUtil.GetLongList(item.ImageUrlIds)[0])).Fpath,
                   AddTime = item.AddTime.ToString("yyyy-mm-dd")
                });
            }
            ViewBag.RecomContent = recomcontents;
            List<CmsContentVm> contenttops = new List<CmsContentVm>();
            foreach (var item in contentlist.Item1)
            {
                contenttops.Add(new CmsContentVm
                {
                    Title = item.Title,
                    SubTitle = item.SubTitle,
                    Summary = item.Summary,
                    CoverUrl = string.Empty,
                    AddTime = item.AddTime.ToString("yyyy-mm-dd")
                }) ;
            }
            ViewBag.TopContent = contenttops;

            var channelhome =await _cmsChannelService.GetModelAsync(a => a.IndexName == "xwzx");

            var channelids =await _cmsChannelService.GetChannelIdsAsync(channelhome.Id, 1);

            var contentnewlist = await  _contentService.PageDynamicList(a => channelids.Contains(a.ChannelId), 1,8, true, a => a.AddTime, a => new {
                a.Id,
                a.Title,
                a.SubTitle,
                a.Summary,
                a.AddTime,
                a.ImageUrlIds
            });
            List<CmsContentVm> contentnews = new List<CmsContentVm>();
            foreach (var item in contentnewlist.Item1)
            {
                contentnews.Add(new CmsContentVm
                {
                    Title = item.Title,
                    SubTitle = item.SubTitle,
                    Summary = item.Summary,
                    CoverUrl =!string.IsNullOrEmpty(item.ImageUrlIds) ?(await _fileService.GetByIdAsync(CommonUtil.GetLongList(item.ImageUrlIds)[0])).Fpath:"",
                    AddTime = item.AddTime.ToString("yyyy-mm-dd")
                });
            }
            ViewBag.NewContentList = contentnews;

            //关于我们
            var channellxwm = await _cmsChannelService.GetModelAsync(a => a.IndexName == "lxwm");
            ViewBag.ChannelLxwm = channellxwm?.Id;
            var contentlxwmmodel = await _contentService.GetFirstModelAsync(a => a.ChannelId== channellxwm.Id,a=>a.Id,true);
            if (contentlxwmmodel!=null) {
                ViewBag.BgLxwm = !string.IsNullOrEmpty(contentlxwmmodel.ImageUrlIds) ? (await _fileService.GetByIdAsync(CommonUtil.GetLongList(contentlxwmmodel.ImageUrlIds)[0])).Fpath : "";
                ViewBag.SummaryLxwm = contentlxwmmodel.Summary;
            }
            return View();
        }
      

        public async Task<ActionResult> NotFound()
        {
            return View();
        }

        #region 公有方法


        /// <summary>
        /// 获取分类文章数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseDataResult GetChannelNum()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            for (int i = 0; i < 6; i++)
            {
                Hashtable hs = new Hashtable ();
                hs.Add("text","分类");
                hs.Add("value", 100 * (i + 1) + new System.Random().Next(1, 99));
                list.Add(hs);
            }
            dr.data = list;
            return dr;
        }

        /// <summary>
        /// 获取分类文章排名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseDataResult GetRankList()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            for (int i = 0; i < 15; i++)
            {
                Hashtable hs = new Hashtable();
                hs.Add("no",i+1);
                hs.Add("title", "测试新闻测试新闻测试新闻测试新闻");
                hs.Add("datetime", DateTime.Now.ToString("yy-MM-dd"));
                list.Add(hs);
            }
            dr.data = list;
            return dr;
        }

        [HttpGet]
        public ResponseDataResult GetBrokenLine()
        {
            ResponseDataResult dr = new ResponseDataResult();
            dr.code = 0;
            List<Hashtable> list = new List<Hashtable>();
            List<string> xAxis = new List<string>();
            List<int> yAxis = new List<int>();
            for (int i = 0; i < 30; i++)
            {
                xAxis.Add((i + 1)+"日");
                yAxis.Add(100 * (i + 1) + new System.Random().Next(1, 99));
            }
            dr.data = new { xAxis, yAxis };
            return dr;
        }


        #endregion

        #region 私有方法
       
        #endregion

    }
}